<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class program extends My_Controller
{
	
	function __construct()
	{
		parent::__construct();
		
		$this->load->model('mdl_master_akun', 'master_akun');		$this->load->helper('form') ;
		
	}
	
	function index()
	{
		$data['can_view'] 	= $this->can_view();
		$data['can_insert'] = $this->can_insert();
		$data['can_update'] = $this->can_update();
		$data['can_delete'] = $this->can_delete();
		
		$this->open();
		   
		
		# load view		//$this->load->view('upload_form', array('error' => ' ' ));
		//$this->load->view('program/program_list', $data, array('error' => ' ' ));
		$file =  $_SERVER['DOCUMENT_ROOT'].'/bp3ti/asset/upload/excel/' ;		//echo $file;		$config['upload_path'] = $file;		$config['allowed_types'] = 'xls';		$config['max_size']	= '1000';				$this->load->library('upload', $config);				if ( ! $this->upload->do_upload())		{			$data['error'] =  $this->upload->display_errors();			$data['excel'] = null;			//echo $data['error'];			$this->load->view('program/program_list', $data);		}			else		{			$upload_data = $this->upload->data();						// Load the spreadsheet reader library			$this->load->library('excel_reader');			// Set output Encoding.			$this->excel_reader->setOutputEncoding('CP1251');			$file =  $file.$upload_data['file_name'] ;			$this->excel_reader->read($file);			error_reporting(E_ALL ^ E_NOTICE);			// Sheet 1			$data['excel'] = $this->excel_reader->sheets[0] ;							$this->load->view('program/program_list', $data);					}
		$this->close();
	}
		function do_upload()	{		$data['can_view'] 	= $this->can_view();		$data['can_insert'] = $this->can_insert();		$data['can_update'] = $this->can_update();		$data['can_delete'] = $this->can_delete();				$this->open();				$file =  $_SERVER['DOCUMENT_ROOT'].'/bp3ti/asset/upload/excel/' ;		//echo $file;		$config['upload_path'] = $file;		$config['allowed_types'] = 'xls';		$config['max_size']	= '1000';				$this->load->library('upload', $config);				if ( ! $this->upload->do_upload())		{			$error = array('error' => $this->upload->display_errors());						echo $error ;		}			else		{			$upload_data = $this->upload->data();									// Load the spreadsheet reader library			$this->load->library('excel_reader');			// Set output Encoding.			$this->excel_reader->setOutputEncoding('CP1251');			$file =  $file.$upload_data['file_name'] ;			$this->excel_reader->read($file);			error_reporting(E_ALL ^ E_NOTICE);			// Sheet 1			$data['excel'] = $this->excel_reader->sheets[0] ;							$this->load->view('program/program_list', $data, array('error' => ' ' ));					}	}
	
	function insert()
	{
		if ($this->can_insert() == FALSE){
			redirect('auth/failed');
		}
		
		$this->open();
		# Data
		$data['AKUNID'] = $this->input->post('kode_akun');
		$data['NAKUN'] = $this->input->post('nama_akun');
		/*		$data['nama_cabang'] = $this->input->post('nama_cabang');
		$data['alamat'] = $this->input->post('alamat');
		$data['telepon'] = $this->input->post('telepon');
		$data['max_piutang'] = $this->input->post('max_piutang');
		$data['saldo_piutang'] = $this->input->post('saldo_piutang');
		$data['userid'] = get_userid();		*/
		
		
		# set rules validation
		$this->form_validation->set_rules('kode_akun', 'Kode Akun', 'required|numeric');
		$this->form_validation->set_rules('nama_akun', 'Nama Akun', 'callback_cek_nama|required');
		/*		$this->form_validation->set_rules('alamat', 'Alamat', 'trim');
		$this->form_validation->set_rules('telepon', 'telepon', 'trim|numeric');
		$this->form_validation->set_rules('max_piutang', 'max_piutang', 'trim|numeric');
		$this->form_validation->set_rules('saldo_piutang', 'saldo_piutang', 'trim|numeric');		*/
		
		
		$this->form_validation->set_error_delimiters('<div class="error">', '</div>');
		
		# set message validation
		$this->form_validation->set_message('required', 'Field %s harus diisi!');
		$this->form_validation->set_message('numeric', 'Field %s harus diisi hanya dengan angka!');
		
		
		if ($this->form_validation->run() == FALSE){
			
			$this->load->view('master_akun/master_akun_add',$data);
			
		}else{	
			$this->master_akun->insert($data);
			$this->session->set_flashdata('message', 'Data Akun Berhasil disimpan.');
			redirect('master_akun');
		}
		
		$this->close();
	}
	
	function cek_nama($str)
	{
		if($str == ''){
			$this->form_validation->set_message('cek_nama', 'Field %s tidak boleh kosong.');
			return FAlSE;
		}
		
		$this->db->flush_cache();
		$this->db->select('*');
		$this->db->where('nama_cabang', $str);
		$q = $this->db->get('cabang');
		
		if($q->num_rows() > 0){
			$this->form_validation->set_message('cek_nama', 'Nama "'. $str . '" sudah terdaftar, coba masukan nama yang lain.');
			return FALSE;
		}else{
			return TRUE;
		}
		
	}
	
	function update($id)
	{
		if ($this->can_update() == FALSE){
			redirect('auth/failed');
		}
			
		$this->open();
		
		$data['result'] 		= $this->master_akun->getItemById($id);
		
		$data['AKUNID'] = $id;
		$data['AKUNID'] = $data['result']->row()->AKUNID;		$data['NAKUN'] = $data['result']->row()->NAKUN;		/*
		$data['alamat'] = $data['result']->row()->alamat;
		$data['telepon'] = $data['result']->row()->telepon;
		$data['max_piutang'] = $data['result']->row()->max_piutang;
		$data['saldo_piutang'] = $data['result']->row()->saldo_piutang;		*/
		
		
		
		$this->load->view('master_akun/master_akun_edit', $data);
		
		$this->close();
	}
	
	function process_update()	{				if ($this->can_update() == FALSE){			redirect('auth/failed');		}				$this->open();				# Data		$data['AKUNID'] = $this->input->post('AKUNID');		$data['NAKUN'] = $this->input->post('NAKUN');		/*		$data['nama_cabang'] = $this->input->post('nama_cabang');		$data['alamat'] = $this->input->post('alamat');		$data['telepon'] = $this->input->post('telepon');		$data['max_piutang'] = $this->input->post('max_piutang');		$data['saldo_piutang'] = $this->input->post('saldo_piutang');		$data['userid'] = get_userid();*/				# set rules validation		$this->form_validation->set_rules('AKUNID', 'Kode Akun', 'required|numeric');		$this->form_validation->set_rules('NAKUN', 'Nama Akun', 'required');/*		$this->form_validation->set_rules('alamat', 'Alamat', 'trim');		$this->form_validation->set_rules('telepon', 'telepon', 'trim|numeric');		$this->form_validation->set_rules('max_piutang', 'max_piutang', 'trim|numeric');		$this->form_validation->set_rules('saldo_piutang', 'saldo_piutang', 'trim|numeric');*/						$this->form_validation->set_error_delimiters('<div class="error">', '</div>');				# set message validation		$this->form_validation->set_message('required', 'Field %s harus diisi!');		$this->form_validation->set_message('numeric', 'Field %s harus diisi hanya dengan angka!');						if ($this->form_validation->run() == FALSE){						$this->load->view('master_akun/master_akun_edit',$data);					}else{				$this->master_akun->update($data['AKUNID'], $data);			$this->session->set_flashdata('message', 'Data Akun Berhasil diupdate.');			redirect('master_akun');		}				$this->close();			}
	
	function delete($id)
	{
		if ($this->can_delete() == FALSE){
			redirect('auth/failed');
		}
		
		$this->master_akun->delete($id);
		$this->session->set_flashdata('message', 'Data Akun Berhasil dihapus.');
		redirect('master_akun');
	}
	
}